package cn.smbms.service.provider;

import java.sql.Connection;
import java.util.List;

import cn.smbms.dao.BaseDao;
import cn.smbms.dao.bill.BillDao;
import cn.smbms.dao.bill.BillDaoImpl;
import cn.smbms.dao.provider.ProviderDao;
import cn.smbms.dao.provider.ProviderDaoImpl;
import cn.smbms.pojo.Bill;
import cn.smbms.pojo.Provider;

public class ProviderServiceImpl implements ProviderService {
	private ProviderDao providerDao;
	private BillDao billDao;
	public ProviderServiceImpl(){
		billDao= new BillDaoImpl();
		providerDao= new ProviderDaoImpl();
	}

	@Override
	public boolean add(Provider provider) {
		
		boolean flag= false;
		Connection connection = null;
		try {
			connection=BaseDao.getConnection();
              connection.setAutoCommit(false);
              flag=providerDao.add(connection, provider);
              connection.commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			try {
				connection.rollback();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}
		finally{
			
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}

	@Override
	public List<Provider> getProviderList(String proName,int pageIndex,int pageSize) {
		Connection connection = null;
		List<Provider> list= null;
		try {
			connection = BaseDao.getConnection();
			list=providerDao.getProviderList(connection, proName, pageIndex, pageSize);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return list;
	}

	@Override
	public int deleteProviderById(String delId) {
		Connection connection= null;
		int billCount=-1;
		try {
			connection=BaseDao.getConnection();
			connection.setAutoCommit(false);
			billCount=billDao.getBillCountByProviderId(connection, delId);
			if(billCount==0){
				providerDao.deleteProviderById(connection, delId);
			}
			connection.commit();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			billCount=-1;
			try {
				connection.rollback();
			} catch (Exception e2) {
				// TODO: handle exception
				e2.printStackTrace();
			}
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return billCount;
	}

	@Override
	public Provider getProviderById(String id) {
		Connection connection=null;
		Provider provider=null;
		try {
			connection=BaseDao.getConnection();
			provider=providerDao.getProviderById(connection, id);
		} catch (Exception e) {
			e.printStackTrace();
			provider=null;
		}
		finally{
			BaseDao.closeResource(connection, null, null);
		}
		return provider;
	}

	@Override
	public boolean modify(Provider provider) {
		boolean flag= false;
		Connection connection =null;
		try {
			connection=BaseDao.getConnection();
			flag=providerDao.modify(connection, provider);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return flag;
	}

	@Override
	public int getCounts(String proName) {
		int counts=0;
		Connection connection=null;
		try {
			connection=BaseDao.getConnection();
			counts=providerDao.getCounts(proName, connection);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			BaseDao.closeResource(connection, null, null);
		}
		return counts;
	}

	
}
